<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">编辑测点</h4>
</div>
<form id="defaultForm" method="post" class="form-horizontal" action="/project/indexManage/index_save">
    <div class="modal-body">
        <div class="form-group">
            <label class="col-lg-3 control-label">编号</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="code" onchange="doVerify()"/>
            </div>
            <sanp id="accountText"></sanp>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">名称</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="name"/>
            </div>
        </div>


        <div class="form-group">
            <label class="col-lg-3 control-label">适用对象</label>
            <div class="col-lg-6">
                <select class="form-control" name="objName">
                    <option th:each="parameter:${objNames}" th:value="${parameter.remark}"
                            th:text="${parameter.name}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">所属类别</label>
            <div class="col-lg-6">
                <select class="form-control" name="objType">
                    <option th:each="parameter:${objTypes}" th:value="${parameter.id}"
                            th:text="${parameter.name}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">数据类型</label>
            <div class="col-lg-6">
                <select class="form-control" name="propertyType">
                    <option th:each="parameter:${propertyTypes}" th:value="${parameter.remark}"
                            th:text="${parameter.name}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">单位</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="unit"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">来源</label>
            <div class="col-lg-6">
                <select class="form-control" name="source">
                    <option th:each="parameter:${sources}" th:value="${parameter.id}" th:text="${parameter.name}">
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">是否展示</label>
            <div class="col-lg-6">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="isShow" value="0" onclick="this.value=(this.value==0)?1:0">
                    </label>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">参数统计方法</label>
            <div class="col-lg-6">
                <select class="form-control" name="esAggsMethod">
                    <option th:each="parameter:${aggsMethod}" th:value="${parameter.id}" th:text="${parameter.name}">
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">接口查询字段名</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="esName"/>
            </div>
        </div>


    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit" id="saveBtn" disabled="false" class="btn btn-primary">保存</button>
    </div>
</form>

<script>
    // initCheck();
    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        }, fields: {
            code: {
                validators: {
                    notEmpty: {
                        message: 'CODE不能为空'
                    }
                }
            },
            name: {
                validators: {
                    notEmpty: {
                        message: '名称不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv', function(e) {
        // 阻止默认提交事件
        e.preventDefault();

        // 获取form表单
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');
        console.log($('#defaultForm').serialize());

        $.ajax({
            url: $form.attr('action'),
            type: "post",
            data: $form.serialize(),
            dataType: 'json',
            success: function (result) {
                if (result.type == "alert") {
                    alert(result.message);
                }
                $("#modal").modal("hide");
                //刷新跳转至第一页
                // $("#dataTable").bootstrapTable("selectPage",1).bootstrapTable("refresh");
                $("#dataTable").bootstrapTable("selectPage", 1);
            },
            error: function () {
                console.log("error")
            }
        });
    });
    window.onload = function () {
        document.getElementById("defaultForm");
    }

    /*****************************************编号唯一验证******************************************/
    function doVerify() {

        var cod = $("input[name ='code']").val()
        var accountText = $("#accountText")[0];
        if (cod != "") {
            $.ajax({
                url: "/project/indexManage/index_code",//后台查询验证的方法
                data: {"code": cod},//携带的参数
                type: "post",
                success: function (data) {
                    //根据后台返回前台的msg给提示信息加HTML
                    if (data) {
                        // 已经存在
                        accountText.innerHTML = "<font color='red' size='3'>" + data + "</font>"
                        //alert(cod);
                        var bbb = $("#saveBtn").attr("disabled",true);

                    }
                    accountText.innerHTML = "<font color='red' size='3'>" + data + "</font>"
                }
            });
        } else {
            accountText.innerText = "";
        }

    }
</script>